﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Dapper;
using MySql.Data.MySqlClient;

namespace SHRMS.childForm
{
    /// <summary>
    /// 部门信息
    /// </summary>
    public partial class childForm_deptInfo : Form
    {
        private IDbConnection dbConnection;
        public childForm_deptInfo()
        {
            InitializeComponent();
            dbConnection = new DbFactory().DefaultDbConnection;
        }

        private void btn_allDeptInfo_Click(object sender, EventArgs e)
        {
            string sql_str = "select * from t_department;";
            var result = dbConnection.Query(sql_str).ToList();
            dataGridView_dept.DataSource = result;
            dataGridView_dept.AutoResizeColumnHeadersHeight();
            dataGridView_dept.AutoResizeColumns();
            dataGridView_dept.AutoResizeRows();
        }

        private void dataGridView_dept_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int index = dataGridView_dept.CurrentRow.Index;
            label_deptNo.Text = dataGridView_dept.Rows[index].Cells[0].Value.ToString();
            textBox_deptname.Text = dataGridView_dept.Rows[index].Cells[1].Value.ToString();
            textBox_address.Text = dataGridView_dept.Rows[index].Cells[2].Value.ToString();
            textBox_telephone.Text = dataGridView_dept.Rows[index].Cells[4].Value.ToString();
            label_count.Text = dataGridView_dept.Rows[index].Cells[3].Value.ToString() + " 人";
        }

        private void comboBox_type_SelectedIndexChanged(object sender, EventArgs e)
        {
            label_sType.Text = comboBox_type.Text + "查询:";
        }

        private void btn_selectDept_Click(object sender, EventArgs e)
        {
            if (label_sType.Text == "按部门名称查询:")
            {
                string strSql = "select * from t_department where deptName like '%" + textBox_str.Text + "%';";
                var result = dbConnection.Query(strSql).ToList();
                dataGridView_dept.DataSource = result;
            }
            else if (label_sType.Text == "按部门编号查询:")
            {
                string strSql = "select * from t_department where departmentNo like '%" + textBox_str.Text + "%';";
                var result = dbConnection.Query(strSql).ToList();
                dataGridView_dept.DataSource = result;
            }
            dataGridView_dept.AutoResizeColumnHeadersHeight();
            dataGridView_dept.AutoResizeColumns();
            dataGridView_dept.AutoResizeRows();
        }

        private void btn_modifyDept_Click(object sender, EventArgs e)
        {
            if (dataGridView_dept.Rows.Count == 0)
            {
                MessageBox.Show("抱歉，您还没有选择相应的部门！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                int index = dataGridView_dept.CurrentRow.Index;
                string deptNo = label_deptNo.Text;
                string deptname = textBox_deptname.Text;
                string deptaddress = textBox_address.Text;
                string depttelephone = textBox_telephone.Text;

                if (deptNo == "0000")
                {
                    MessageBox.Show("抱歉，您还没有选择相应的部门！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    string strSql = "UPDATE t_department SET deptName = '" + deptname + "',address = '" + deptaddress + "',telephone = '" + depttelephone + "' WHERE departmentNo = '" + deptNo + "';";
                    var result = dbConnection.Execute(strSql);
                    if (result > 0)
                    {
                        MessageBox.Show("修改部门成功！", "修改成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        MessageBox.Show("修改部门失败！", "修改失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
        }

        private void btn_deleteDept_Click(object sender, EventArgs e)
        {
            if (dataGridView_dept.Rows.Count == 0)
            {
                MessageBox.Show("抱歉，您还没有选择相应的部门！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                //int index = dataGridView_dept.CurrentRow.Index;
                string deptNo = label_deptNo.Text;

                if (deptNo == "0000")
                {
                    MessageBox.Show("抱歉，您还没有选择相应的部门！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    DialogResult resault = MessageBox.Show("确认要删除该部门？\n删除该部门后则该部门的员工信息也将会被删除！", "删除警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation);
                    if (resault == DialogResult.Cancel)
                    {
                        MessageBox.Show("删除已取消！", "删除提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }
                    string strSql = "DELETE FROM t_department WHERE departmentNo =  '" + deptNo + "';";
                    var result = dbConnection.Execute(strSql);
                    if (result > 0)
                    {
                        MessageBox.Show("删除部门成功！", "删除成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        MessageBox.Show("删除部门失败！", "删除失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
        }
    }
}
